<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
        <style>
            .avatar-uploader .el-upload {
                border: 1px dashed #d9d9d9;
                border-radius: 6px;
                cursor: pointer;
                position: relative;
                overflow: hidden;
            }
            .avatar-uploader .el-upload:hover {
                border-color: #409EFF;
            }
            .avatar-uploader-icon {
                font-size: 28px;
                color: #8c939d;
                width: 178px;
                height: 178px;
                line-height: 178px;
                text-align: center;
            }
            .avatar {
                width: 178px;
                height: 178px;
                display: block;
            }
            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }
            .datatable td, .datatable th {
                padding: 12px 0;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: left;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>系统管理<small>角色管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                    <el-breadcrumb-item>角色管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="角色名称/角色关键字" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                    <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="id" align="center" label="角色id"></el-table-column>
                        <el-table-column prop="name" align="center" label="角色名称"></el-table-column>
                        <el-table-column prop="keyword" align="center" label="角色关键字"></el-table-column>
                        <el-table-column prop="description" align="center" label="角色说明"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                                <el-button type="warning" size="mini" @click="handle(scope.row)">权限菜单</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                                class="pagiantion"
                                @current-change="handleCurrentChange"
                                :current-page="pagination.currentPage"
                                :page-size="pagination.pageSize"
                                layout="total, prev, pager, next, jumper"
                                :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                            <template>
                                <el-form label-position="right" label-width="100px" :model="formData" :rules="formRule">
                                    <el-row>
                                    <el-col :span="18">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input v-model="formData.name" />
                                            </el-form-item>
                                        </el-col>
                                </el-row>
                                    <el-row>
                                    <el-col :span="18">
                                            <el-form-item label="角色关键字" prop="keyword">
                                                <el-input placeholder="控制器中定义的权限字符,例:ROLE_ADMIN" v-model="formData.keyword" />
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                            <el-col :span="24">
                                                <el-form-item label="角色说明">
                                                    <el-input  v-model="formData.description" type="textarea"></el-input>
                                                </el-form-item>
                                            </el-col>
                                    </el-row>
                                </el-form>

                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                            <template>
                                <el-row>
                                    <el-form label-position="right" label-width="100px" :model="formData" :rules="formRule">
                                        <el-col :span="12">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input placeholder="" v-model="formData.name" />
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="角色关键字" prop="keyword">
                                                <el-input v-model="formData.keyword" />
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="24">
                                            <el-form-item label="角色说明">
                                                <el-input  v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-form>
                                </el-row>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>


                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="权限菜单" :visible.sync="dialogFormVisibleNEW">
                            <template>
                                <el-tabs v-model="activeName" type="card">
                                    <el-tab-pane label="权限分配" name="first">
                                        <el-form label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="角色名称">
                                                        <el-input v-model="formData.name" :disabled="true"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="关键字">
                                                        <el-input v-model="formData.keyword" :disabled="true"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择权限</th>
                                                    <th>权限ID</th>
                                                    <th>权限名称</th>
                                                    <th>权限说明</th>
                                                    <th>权限描述</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="r in tableData">
                                                    <td>
                                                        <input :id="r.id" v-model="checkIds" type="checkbox" :value="r.id">
                                                    </td>
                                                    <td><label :for="r.id">{{r.id}}</label></td>
                                                    <td><label :for="r.id">{{r.name}}</label></td>
                                                    <td><label :for="r.id">{{r.keyword}}</label></td>
                                                    <td><label :for="r.id">{{r.permission}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                    <el-tab-pane label="菜单分配" name="second">
                                        <el-form label-position="right" label-width="100px">
                                            <el-row>
                                                <el-col :span="12">
                                                    <el-form-item label="角色名称">
                                                        <el-input v-model="formData.name" :disabled="true"/>
                                                    </el-form-item>
                                                </el-col>
                                                <el-col :span="12">
                                                    <el-form-item label="关键字">
                                                        <el-input v-model="formData.keyword" :disabled="true"/>
                                                    </el-form-item>
                                                </el-col>
                                            </el-row>
                                        </el-form>
                                        <div class="checkScrol">
                                            <table class="datatable">
                                                <thead>
                                                <tr>
                                                    <th>选择菜单</th>
                                                    <th>菜单ID</th>
                                                    <th>菜单名称</th>
                                                    <th>链接地址</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <tr v-for="r in tableData2">
                                                    <td>
                                                        <input :id="r.id" v-model="checkIds2" type="checkbox" :value="r.id">
                                                    </td>
                                                    <td><label :for="r.id">{{r.id}}</label></td>
                                                    <td><label :for="r.id">{{r.name}}</label></td>
                                                    <td><label :for="r.id">{{r.linkUrl}}</label></td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </el-tab-pane>
                                </el-tabs>
                            </template>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisibleNEW = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit2()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>


                </div>
            </div>
        </div>
    </body>

    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                checkIds:[],
                checkIds2:[],
                tableData: [],
                tableData2: [],
                activeName: 'first',
                formRule:{
                    name:[{required:true,message:'角色名称不能为空',trigger:'blur'}],
                    keyword:[{required:true,message:'角色关键字不能为空',trigger:'blur'}],
                },
                pagination: {//分页相关属性
                    currentPage: 1,
                    pageSize:10,
                    total:100,
                    queryString:null,
                },
                dataList: [],//列表数据
                formData: {
                    name:'',
                    keyword:'',
                },//表单数据
                dialogFormVisible: false,//控制添加窗口显示/隐藏
                dialogFormVisible4Edit:false,//控制编辑窗口显示/隐藏
                dialogFormVisibleNEW:false,//控制编辑窗口显示/隐藏
            },
            created() {
                this.findPage();
            },

            methods: {
                //权限菜单确定
                handleEdit2() {
                    axios.post("/setting/fenPei.do?checkIds="+this.checkIds,this.formData).then(res=>{
                        this.dialogFormVisibleNEW =false;
                        //成功或失败提示 刷新页面
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success':'error'
                        });
                    }).catch((error)=>{
                        this.showMessage(error);
                    });
                    axios.post("/setting/fenPei2.do?checkIds2="+this.checkIds2,this.formData).then(res=>{
                        this.dialogFormVisible =false;
                        //成功或失败提示 刷新页面
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success':'error'
                        });
                    }).catch((error)=>{
                        this.showMessage(error);
                    });

                },
                //弹出菜单窗口
                handle(row) {
                    this.dialogFormVisibleNEW = true;
                    //根据id查询角色数据
                    axios.get("/setting/findById.do?id="+row.id).then(res=>{
                        this.formData =  res.data.data;
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success':'error'
                        });
                       //查询所有权限数据
                    axios.get("/setting/findPermissionAll.do?id="+row.id).then(res=>{
                        this.tableData =res.data;
                        //根据角色id查询权限ids

                        axios.get("/setting/findPrIdsByid.do?id="+row.id).then(res=>{
                            this.checkIds = res.data;
                        })
                    })
                        //查询菜单数据
                        //查询所有权限数据
                        axios.get("/setting/findMenuAll.do?id="+row.id).then(res=>{
                            this.tableData2 =res.data;
                            //根据角色id查询权限ids

                            axios.get("/setting/findMnIdsByid.do?id="+row.id).then(res=>{
                                this.checkIds2 = res.data;
                            })
                        })
                    })
                },
                //弹出编辑窗口
                handleUpdate(row) {
                    //弹出编辑窗口
                    this.dialogFormVisible4Edit = true;
                    //根据id查询角色数据
                    axios.get("/setting/findById.do?id="+row.id).then(res=>{
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success':'error'
                        });
                        this.formData =  res.data.data;
                    })
                },
                //编辑
                handleEdit() {
                    axios.post("/setting/edit",this.formData).then(res=>{
                        this.dialogFormVisible4Edit =false;
                        //成功或失败提示 刷新页面
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success':'error'
                        });
                        this.findPage();
                    }).catch((error)=>{
                        this.showMessage(error);
                    });
                },

                showMessage(error){
                    if(error == 'Error: Request failed with status code 403'){
                        //权限不足
                        this.$message.error('无访问权限');
                        return;
                    }else{
                        this.$message.error('未知错误');
                        return;
                    }
                },
                //删除
                handleDelete(row) {
                    this.$confirm('此操作将永久删除该套餐记录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.get("/setting/deleteById.do?id="+row.id).then(res=>{
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success':'error'
                            });
                            this.findPage();
                        }).catch((error)=>{
                            this.showMessage(error);
                        });
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                },
                //添加
                handleAdd() {
                    //1.验证（省略）
                    //2.提交表单数据
                    axios.post("/setting/add.do?name="+this.formData.name,this.formData).then(res=>{
                        this.resetForm();
                        //弹出窗口
                        this.dialogFormVisible = false;
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success':'error'
                        });
                        this.findPage();//分页查询
                    })
                },
                //分页查询
                findPage() {
                    var queryPageBean = {
                        currentPage:this.pagination.currentPage,
                        pageSize:this.pagination.pageSize,
                        queryString:this.pagination.queryString
                    };

                    axios.post("/setting/findPage4role.do",queryPageBean).then(res=>{
                        this.pagination.total =  res.data.total;
                        this.dataList = res.data.rows;
                    }).catch((error)=>{
                        this.showMessage(error);
                    });
                },

                // 重置表单
                resetForm() {
                    this.formData = {
                    };
                    this.tableData = [];
                },
                // 弹出添加窗口
                handleCreate() {
                    this.dialogFormVisible = true;
                    //1.重置表单
                    this.resetForm();
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;
                    this.findPage();
                }
            }
        })
    </script>
</html>
